
//
//  WlisNetWrokTools.swift
//  Swift_missScarlett
//
//  Created by xmg on 16/8/16.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

import AFNetworking

enum RequestType : String{
    case GET = "GET"
    case POST = "POST"
}

class WlisNetWrokTools: AFHTTPSessionManager {

    static let shareIntance : WlisNetWrokTools = {
        let tools = WlisNetWrokTools();
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools
    }()
}

extension WlisNetWrokTools{

    /** 封装网络请求的封装 */
    func request(type : RequestType ,URLString: String, parameters: [String :NSObject],finished :(progress : NSProgress?,result: AnyObject?, error : NSError?)-> Void){
        
        /** 进度闭包 */
        let progressCallBack = { (progress : NSProgress) -> Void in
            finished(progress: progress, result: nil, error: nil)
        }
        /** 成功闭包 */
        let successCallBack = { (task : NSURLSessionDataTask, result :AnyObject?) -> Void in
            finished(progress: nil, result: result, error: nil)
        }
        /** 失败闭包 */
        let failureCallBack = { (task : NSURLSessionDataTask?, error : NSError) -> Void in
            finished(progress: nil, result: nil, error: error);
        }
        /** 判断是请求 */
        switch type {
        case .GET :
            GET(URLString, parameters: parameters, progress: progressCallBack, success: successCallBack, failure: failureCallBack)
        case .POST :
            POST(URLString, parameters: parameters, progress: progressCallBack, success: successCallBack, failure: failureCallBack)
        }
        
    }
}